<?php
namespace Modules\Frontend\Controllers;

class AuthController extends \Phalcon\Mvc\Controller {
	public function beforeExecuteRoute() {
		\Components\Utils::isClosedFrontent($this);
	}

	public function initialize() {
		$this -> response -> setStatusCode(200, 'OK') -> sendHeaders() -> setContentType('application/json', 'UTF-8');
	}

	public function indexAction() {
		$validation = $this -> account::getValidationInstance()
		-> addAccount()
		-> addPassword()
		-> addCaptcha()
		-> execute($this -> request -> getPost());
		if($validation['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$validation['code'],'msg'=>$validation['message']]);
		}
		/*
		if(!$this -> session -> has('frontendCaptcha') || ($this -> session -> get('frontendCaptcha') != $validation['data']['captcha'])) {
			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'验证码错误']);
		}
		*/

		$service = $this -> account::getServiceInstance() -> loginToken($validation['data']);
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>$service['message']]);
		}
/*
		if($service['account'] -> create_time + 172800 < time()) {
			$serviceTradeSell = $this -> trade::getSellServiceInstance() -> findFirst([
				'conditions' => 'status in(0,1,2,3,4) AND from_user_id = ' . $service['account'] -> user_id
			]);
			if($serviceTradeSell['code'] != 0) {
				$serviceTradeBuy = $this -> trade::getBuyServiceInstance() -> findFirst([
					'conditions' => 'from_user_id = ' . $service['account'] -> user_id,
					'order' => 'trade_buy_id DESC'
				]);
				if($serviceTradeBuy['code'] != 0) {
					$service['account'] -> status = 3;
					$service['account'] -> save();
				} else if(
					($serviceTradeBuy['trade_buy'] -> status == 6 || $serviceTradeBuy['trade_buy'] -> status == 7) &&
					$serviceTradeBuy['trade_buy'] -> create_time + 172800 < time()
				) {
					$service['account'] -> status = 3;
					$service['account'] -> save();
				}
			}
		}
*/
		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'登陆成功','token'=>$service['token']]);
	}

	public function forgetPasswordAction() {
		$validation = $this -> account::getValidationInstance()
		-> addCellphone()
		-> addPassword()
		-> addCaptcha()
		-> execute($this -> request -> getPost());
		if($validation['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$validation['code'],'msg'=>$validation['message']]);
		}

		if(base64_decode($validation['data']['captchaToken']) != md5($validation['data']['captcha'])) {
			return $this -> response -> setJsonContent(['code'=>-1,'msg'=>'验证码错误']);
		}

		$service = $this -> account::getServiceInstance() -> findFirst('cellphone = "' . $validation['data']['cellphone'] . '"');
		if($service['code'] != 0) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>'此手机号下的用户不存在']);
		}

		$service['user'] -> password = $this -> security -> hash($validation['data']['password']);
		if(!$service['user'] -> save()) {
			return $this -> response -> setJsonContent(['code'=>$service['code'],'msg'=>'修改失败']);
		}
		return $this -> response -> setJsonContent(['code'=>0,'msg'=>'修改成功']);
	}
}